package cn.zwxky.android.user.My;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.net.MD5;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.SPUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;
import com.zgscwjm.lsfbbasetemplate.utils.valid.Valid;

import java.util.HashMap;

import cn.zwxky.android.user.Login.ForgetBean;
import cn.zwxky.android.user.Login.LoginActivity;
import cn.zwxky.android.user.R;
import cn.zwxky.android.user.Utils.LittleUtils;
import cn.zwxky.android.user.Utils.NoDoubleClickUtils;
import cn.zwxky.android.user.Utils.URLString;

/**
 * 密码修改页面
 * Created by Administrator on 2017/3/15 0015.
 */
@ContentView(R.layout.aty_pwd)
public class PwdActivity extends LsfbActivity {

    /**
     * 旧密码
     */
    @ViewInject(R.id.aty_my_details_et_old_pwd)
    private EditText et_old_pwd;

    /**
     * 新密码
     */
    @ViewInject(R.id.aty_my_details_et_pwd1)
    private EditText et_pwd1;

    /**
     * 确认新密码
     */
    @ViewInject(R.id.aty_my_details_et_pwd2)
    private EditText et_pwd2;

    @Override
    public void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        LittleUtils.setsimpletitlebar(this, "密码管理");

    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_my_details_btn_post})
    public void onClick(View view) {
        switch (view.getId()) {
            //立即修改
            case R.id.aty_my_details_btn_post:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_old_pwd.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_pwd2.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_pwd1.getWindowToken(), 0);
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    editPwd();
                }
                break;
        }
    }

    public void editPwd() {
        if (et_old_pwd.getText().toString().isEmpty()) {
            SN.SnackShow(et_old_pwd, "请输入原密码");
            return;
        }
        if (et_pwd1.getText().toString().isEmpty()) {
            SN.SnackShow(et_old_pwd, "请输入新密码");
            return;
        }
        if (et_pwd2.getText().toString().isEmpty()) {
            SN.SnackShow(et_old_pwd, "请再次输入新密码");
            return;
        }
        if (!et_pwd2.getText().toString().equals(et_pwd1.getText().toString())) {
            SN.SnackShow(et_old_pwd, "两次新密码输入不一致，请重新输入");
            et_pwd1.setText("");
            et_pwd2.setText("");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("opwd", MD5.GetMD5Code(et_old_pwd.getText().toString()));
        hashMap.put("npwd", MD5.GetMD5Code(et_pwd1.getText().toString()));
        new BaseInternet().getData(URLString.EDIT_PWD, hashMap, ForgetBean.class, new EditPwdEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void EditPwdEvent(EditPwdEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:
                Config.setUserId("");
                LittleUtils.exit(this);
                T.showShort(this,event.getMsg());
                SPUtils.remove(this, "et_password");
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                SN.SnackShow(et_pwd1, event.getMsg());
                break;
        }
    }

    @Override
    public void initData() {

    }
}
